/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_RTP_TCP_SERVER_H
#define MK_RTP_TCP_SERVER_H
#include "MkTcp/MkTcpServer.h"
#include "MkTcp/MkTcpSession.h"
#include "MkRtp.h"

class MkApi CMkRtpTcpSession : public CMkTcpSession
{
public:
    CMkRtpTcpSession(CMkEvent* pEvent, CMkTimer* pTimer, CMkTcpServer* pServer);
    ~CMkRtpTcpSession();
    void SetSsrc(const Uint32& Ssrc) { m_Ssrc = Ssrc; }
    const Uint32& GetSsrc() const { return m_Ssrc; }
protected:
    Uint32 OnRecv() override;
private:
    Uint8*              m_pRecvBuffer;
    Uint32              m_nMaxLen;
    Uint32              m_nOffset;
    Uint32              m_Ssrc;
};

class CMkRtpTcpServer : public CMkTcpServer, public CMkRtp
{
public:
    CMkRtpTcpServer(CMkEventPool* pEventPool, CMkTimer* pTimer);
    ~CMkRtpTcpServer();

    Uint32 ParsePacket(Uint8* pBuf, Uint32 nLen, const MkString& SenderHost, const Uint16& SenderPort, CMkRtpTcpSession* pRtpSession);
    Uint32 SendRtpPacket(const CMkRtpPacket& RtpPacket, const MkString& RemoteIp, Uint16 RemotePort) override;
    Uint32 SendRtcpPacket(const CMkRtcpPacket& RtcpPacket, const MkString& RemoteIp, Uint16 RemotePort) override;
    Uint32 DeleteRemoter(const Uint32& Ssrc) override;
protected:
    CMkTcpSession* CreateNewSession(CMkEvent *pEvent, CMkTimer* pTimer) override;
    Uint32 OnSessionAccepted(CMkTcpSession *pSession) override;
    Uint32 OnSessionEventRemoved(CMkTcpSession* pSession) override;
    Uint32 Release() override;
private:
    CMkRwLock                                   m_RtpSessionLock;
    MkUnorderedMap<Uint32, CMkRtpTcpSession*>   m_MapRtpSession; // key ssrc
};

#endif